package com.example.a2022application.activity;

import android.content.Intent;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;

import com.example.a2022application.BaseActivity;
import com.example.a2022application.MainActivity;
import com.example.a2022application.R;
import com.example.a2022application.bean.ClassesBean;
import com.example.a2022application.bean.RegisterBean;
import com.example.a2022application.net.Urls;
import com.example.a2022application.utils.ToastUtil;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.Nullable;

public class AddTimeTableActivity extends BaseActivity {
    private EditText et_courseName,et_week,et_start,et_end;
    private Button btn_submit;
    private Spinner spin_class;

    private String classes = "";

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_addtimetable);
        spin_class = findViewById(R.id.spin_class);
        et_courseName = findViewById(R.id.et_courseName);
        et_week = findViewById(R.id.et_week);
        et_start = findViewById(R.id.et_start);
        et_end = findViewById(R.id.et_end);
        btn_submit = findViewById(R.id.btn_submit);
        getClasses();
        btn_submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String courseName = et_courseName.getText().toString();
                String week = et_week.getText().toString();
                String start = et_start.getText().toString();
                String end = et_end.getText().toString();
                if (TextUtils.isEmpty(classes)){
                    ToastUtil.showToast("请选择班级名称");
                    return;
                }
                if (TextUtils.isEmpty(courseName)){
                    ToastUtil.showToast("请输入课程名称");
                    return;
                }
                if (TextUtils.isEmpty(week)){
                    ToastUtil.showToast("请输入上课星期");
                    return;
                }if (TextUtils.isEmpty(start)){
                    ToastUtil.showToast("请输入课程开始节数");
                    return;
                }
                if (TextUtils.isEmpty(end)){
                    ToastUtil.showToast("请输入课程结束节数");
                    return;
                }

                OkGo.<String>post(Urls.ADDTIMETABLE)
                        .params("classname", classes)
                        .params("courseName",courseName)
                        .params("week",week)
                        .params("start_node",start)
                        .params("end_node",end)
                        .execute(new StringCallback() {
                            @Override
                            public void onSuccess(Response<String> response) {
                                try {
                                    RegisterBean bean = getGson().fromJson(response.body(), RegisterBean.class);
                                    if (bean.getCode()==0){
                                        finish();
                                    }
                                    ToastUtil.showToast(bean.getMessage());
                                }catch (Exception e){

                                }
                            }
                        });

            }
        });
    }
    private void getClasses() {
        OkGo.<String>post(Urls.SELECTCLASS)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        ClassesBean bean = getGson().fromJson(response.body(), ClassesBean.class);
                        if (bean.getCode() == 0){
                            List<String> strarr = new ArrayList<>();
                            for (int i = 0;i<bean.getData().size();i++){
                                strarr.add(bean.getData().get(i).getClass_name());
                            }
                            ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(AddTimeTableActivity.this,
                                    android.R.layout.simple_spinner_item, strarr);
                            dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                            spin_class.setAdapter(dataAdapter);
                            spin_class.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                                @Override
                                public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                                    classes = strarr.get(i);
                                }

                                @Override
                                public void onNothingSelected(AdapterView<?> adapterView) {

                                }
                            });
                        }
                    }
                });
    }
    //返回
    public void back(View view){
        finish();
    }
}
